<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" clearable placeholder="请输入订单号" style="width: 240px" @change="handQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="statusList">
        <el-select
          v-model="queryParams.statusList"
          multiple
          clearable
          collapse-tags
          collapse-tags-tooltip
          placeholder="请选择状态"
          style="width: 240px"
          @change="handQuery"
          :max-collapse-tags="2"
        >
          <el-option label="待服务" :value="1"></el-option>
          <el-option label="已完成" :value="2"></el-option>
          <el-option label="已取消" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="医院名称" prop="itemHospitalName">
        <el-select
          v-model="queryParams.itemHospitalName"
          clearable
          filterable
          placeholder="请选择医院名称"
          style="width: 240px"
          @change="handQuery"
        >
          <el-option
            v-for="h in hospitalOptions"
            :key="h.id"
            :label="h.name"
            :value="h.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="就诊人姓名" prop="itemPatientName">
        <el-input v-model="queryParams.itemPatientName" clearable placeholder="请输入就诊人姓名" style="width: 240px" @change="handQuery" />
      </el-form-item>
      <el-form-item label="陪诊师姓名" prop="attendantName">
        <el-input v-model="queryParams.attendantName" clearable placeholder="请输入陪诊师姓名" style="width: 240px" @change="handQuery" />
      </el-form-item>
      <el-form-item label="更新时间" prop="dateRange" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="YYYY-MM-DD"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="takeOrderList">
      <el-table-column label="订单号" align="center" prop="orderNo" min-width="100" v-if="columns.orderNo.visible"/>
      <el-table-column label="医院名称" align="center" prop="itemHospitalName" min-width="120" show-overflow-tooltip v-if="columns.itemHospitalName.visible" />
      <el-table-column label="服务项目" align="center" prop="itemName" show-overflow-tooltip v-if="columns.itemName.visible" />
      <el-table-column label="就诊人姓名" align="center" prop="itemPatientName" v-if="columns.itemPatientName.visible" />
      <el-table-column label="陪诊师姓名" align="center" prop="attendantName" v-if="columns.attendantName.visible">
        <template #default="scope">
          <span>{{ scope.row.attendantName || '--'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计收益" align="center" prop="expectedIncome" v-if="columns.expectedIncome.visible">
        <template #default="scope">
          <span style="color: #7CBF50;">¥{{ centsToYuan(scope.row.expectedIncome) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" v-if="columns.status.visible">
        <template #default="scope">
          <el-tag :type="getOrderStatusTagType(scope.row.status)">{{ getOrderStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态更新时间" align="center" prop="updateTime" width="200" v-if="columns.updateTime.visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="columns.action.visible">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 接单详情对话框 -->
    <TakeOrderDetailDialog 
      :detailDialogVisible="detailDialogVisible" 
      :orderId="currentOrderId" 
      @handleClose="handleClose"
    />
  </div>
</template>

<script setup name="TakeOrder">
import { listTakeOrder } from "@/api/takeOrder/takeOrder";
import TakeOrderDetailDialog from "./components/TakeOrderDetailDialog.vue";
import { useRoute } from 'vue-router';
import { centsToYuan } from '@/utils/money'
import { listHospital } from '@/api/hospital/hospital'

const { proxy } = getCurrentInstance();
const route = useRoute();

const takeOrderList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);
const detailDialogVisible = ref(false);
const currentOrderId = ref(null);
const hospitalOptions = ref([]);

// 列显隐控制
const columns = ref({
  orderNo: { label: "订单号", visible: true },
  itemHospitalName: { label: "医院名称", visible: true },
  itemName: { label: "服务项目", visible: true },
  itemPatientName: { label: "患者姓名", visible: true },
  expectedIncome: { label: "预计收益", visible: true },
  attendantName: { label: "陪诊师姓名", visible: true },
  status: { label: "状态", visible: true },
  updateTime: { label: "状态更新时间", visible: true },
  action: { label: "操作", visible: true }
});

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    statusList: [],
    itemHospitalName: null,
    itemPatientName: null,
    attendantName: null,
    startTime: null,
    endTime: null
  }
});

const { queryParams } = toRefs(data);

// 订单状态映射
const getOrderStatusText = (status) => {
  const statusMap = {
    1: '待服务',
    2: '已完成',
    3: '已取消'
  }
  return statusMap[status] || '--';
}

const getOrderStatusTagType = (status) => {
  const statusMap = {
    1: 'primary',
    2: 'success',
    3: 'danger'
  }
  return statusMap[status] || 'info';
}
/** 查询接单记录列表 */
function getList() {
  loading.value = true;
  if (dateRange.value && dateRange.value.length === 2) {
    queryParams.value.startTime = dateRange.value[0] + ' 00:00:00'
    queryParams.value.endTime =  dateRange.value[1] + ' 00:00:00'
  }else {
    queryParams.value.startTime = null;
    queryParams.value.endTime = null;
  }
  listTakeOrder(queryParams.value).then((res) => {
    takeOrderList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handQuery();
}

/** 查看详情按钮操作 */
function handleView(row) {
  currentOrderId.value = row.id;
  detailDialogVisible.value = true;
}

function handleClose() {
  detailDialogVisible.value = false;
  currentOrderId.value = undefined;
}

onMounted(() => {
  if (route.query.attendantName) {
    queryParams.value.attendantName = route.query.attendantName;
  }
  getList();
  // 预加载医院列表（大分页）
  listHospital({ pageNum: 1, pageSize: 2000 }).then(res => {
    hospitalOptions.value = res.rows || []
  })
});
</script>
